interface IMemoryCacheValue<T> {
    value: T;
    expires: number;
}

export default class MemoryCache {
    private constructor() {
    }

    private static cache: { [prop: string]: IMemoryCacheValue<any> } = {};

    public static get<T>(key: string) {
        const cacheData = this.cache[key];
        if (!!cacheData) {
            if ((0 === cacheData.expires || Date.now() < cacheData.expires)) {
                return cacheData.value as T;
            } else {
                delete this.cache[key];
            }
        }
        return null;
    }

    public static set(key: string, value: any, availableForever: boolean = false) {
        this.cache[key] = {
            value,
            expires: !availableForever ? Date.now() : 0,
        };
    }

    public static remove(key: string) {
        delete this.cache[key];
    }
}